package com.bestcem.xm.ticket.controller;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.controller.vo.SimpleSurveyVO;
import com.bestcem.xm.ticket.service.FilterService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 问卷缩略信息过滤接口
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/31 10:44
 */
@Api(tags = "问卷缩略信息过滤接口")
@RestController
@RequestMapping(value = "/ticket/tickets/survey", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketsSurveyFilterController {

    @Resource
    private FilterService filterService;
    @Resource
    private HttpServletRequest request;

    /**
     * 获取公司所有工单对应的问卷缩略信息
     *
     * @param page 页号
     * @param size 大小
     * @return 分页DTO
     * @author zq.ding <zq.ding@idiaoyan.com>
     * @date 2021/8/18 21:50
     */
    @ApiOperation(value = "获取公司所有工单对应的问卷缩略信息")
    @GetMapping(value = "/filter/")
    public XmResultJson<PageDTO> surveyFilter(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "rowsPerPage", required = false) Integer size) {
        String orgId = SecurityContextHolder.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT, "公司id格式错误");
        }
        if (Objects.isNull(page)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少page参数");
        }
        if (Objects.isNull(size)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少rowsPerPage参数");
        }
        if (page <= 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "page不在范围内");
        }
        if (size <= 0 || size >= 999) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "rowsPerPage不在范围内");
        }

        PageParam pageParam = new PageParam(page, size);

        ServiceResult<PageDTO<SimpleSurveyVO>> result = filterService.surveyFilter(orgId, pageParam);
        if (result.isSuccess()) {
            return XmResultJson.success(result.getData());
        }
        return XmResultJson.fail(result.getErrorData(), result.getMsg());
    }
}
